#ifndef CNSREF_H
#define CNSREF_H

#include <QAtomicInteger>
#include "cnsMacroGlobal.h"
#include "cnssharedpointer.h"

/**
 * CnsRef is used for reference count management. If a class inherits from CnsRef,
 * then it is easy to be shared in different places.
 * @js NA
 */
class CnsRef
{
public:
    /**
     * Retains the ownership.
     *
     * This increases the CnsRef's reference count.
     *
     * @see release, autorelease
     * @js NA
     */
    void retain();

    /**
     * Releases the ownership immediately.
     *
     * This decrements the CnsRef's reference count.
     *
     * If the reference count reaches 0 after the decrement, this CnsRef is
     * destructed.
     *
     * @see retain, autorelease
     * @js NA
     */
    void release();

    /**
     * Returns the CnsRef's current reference count.
     *
     * @returns The CnsRef's reference count.
     * @js NA
     */
    unsigned int getReferenceCount() const;

protected:
    /**
     * Constructor
     *
     * The CnsRef's reference count is 1 after construction.
     * @js NA
     */
    CnsRef();

public:
    /**
     * Destructor
     *
     * @js NA
     * @lua NA
     */
    virtual ~CnsRef();

protected:
    /// count of references
    QAtomicInteger<uint32_t> _referenceCount;

};

CNS_COM_DECL_METATYPE(CnsRef)

#endif // CNSREF_H
